package com.example.shuiyinceshi

import android.content.ContentValues
import android.util.Log
import com.arthenica.ffmpegkit.FFmpegKit
import com.arthenica.ffmpegkit.FFprobeKit
import com.arthenica.ffmpegkit.ReturnCode

class MyFFmpegKit {

    //获取视频码率
    fun getbitrate():Int
    {
        var session=FFprobeKit.execute("-v error -show_entries format=bit_rate -of default=noprint_wrappers=1:nokey=1 '/storage/emulated/0/1/m.mp4'")
        var bitrate=session.output.toInt()
        //Log.d(ContentValues.TAG,"123456789   " +session.output);
        if (bitrate>=0)
        {
            return bitrate
        }

        return 0

    }
    fun  imageaddtxt():Boolean
    {

        //%{localtime}
        ///storage/emulated/0/Android/data/com.example.shuiyinceshi/cache/black.ttf'

        //FFmpegKit.execute("-formats")
        //-b:v 2048k -bufsize 2048k
        //-q:v 0 0-50无损
        //-c:v h264

        var session=FFmpegKit.execute("-i /storage/emulated/0/1/m.mp4 -vf drawtext=text=\\'时间' '2024.09.29' '17:36\\':fontfile='/storage/emulated/0/1/black.ttf':x=10:y=h-60:fontsize=48:fontcolor=white:shadowy=2 -c:v h264 -b:v 1500K -c:a copy -y /storage/emulated/0/1/m2.mp4")

        //var session=FFmpegKit.execute("-i '/storage/emulated/0/1/m.mp4'")

        //session.output
        Log.d(ContentValues.TAG, "12313131 "+session.output);
        if (ReturnCode.isSuccess(session.getReturnCode())) {

            // SUCCESS
            return true

        } else if (ReturnCode.isCancel(session.getReturnCode())) {

            // CANCEL
            return false

        } else {

            // FAILURE
            Log.d(ContentValues.TAG, String.format("Command failed with state %s and rc %s.%s", session.getState(), session.getReturnCode(), session.getFailStackTrace()));

        }
        return false
    }

    fun  imageaddtxt2():Boolean
    {
        var session=FFmpegKit.execute("-i /storage/emulated/0/1/1.jpg -vf [i]drawtext=text=\\'时间' '2024.09.29' '17:36\\':fontfile='/storage/emulated/0/1/black.ttf':x=100:y=h-300:fontsize=48:fontcolor=white:shadowy=2,drawtext=text=\\'时间' '2024.09.29' '17:36\\':fontfile='/storage/emulated/0/1/black.ttf':x=100:y=h-250:fontsize=48:fontcolor=white:shadowy=2[out]  -y /storage/emulated/0/1/2.jpg")

        if (ReturnCode.isSuccess(session.getReturnCode())) {

            // SUCCESS
            return true

        } else if (ReturnCode.isCancel(session.getReturnCode())) {

            // CANCEL
            return false

        } else {

            // FAILURE
            Log.d(ContentValues.TAG, String.format("Command failed with state %s and rc %s.%s", session.getState(), session.getReturnCode(), session.getFailStackTrace()));

        }
        return false
    }

    fun videoaddtxt()
    {
        var session=FFmpegKit.execute("-i /storage/emulated/0/1/m.mp4 " +
                "-vf [i]drawtext=text='999：666':fontfile='/storage/emulated/0/Android/data/com.example.shuiyinceshi/cache/arial.ttf':" +
                "x=100:y=h-300:fontsize=48:fontcolor=white:shadowy=2," +
                "drawtext=text='n100 s200':fontfile='/storage/emulated/0/Android/data/com.example.shuiyinceshi/cache/arial.ttf':"+
                "x=100:y=h-250:fontsize=48:fontcolor=white:shadowy=2[out] "+
                "-c:a copy  -y /storage/emulated/0/1/m2.mp4")
        if (ReturnCode.isSuccess(session.getReturnCode())) {

            // SUCCESS

        } else if (ReturnCode.isCancel(session.getReturnCode())) {

            // CANCEL

        } else {

            // FAILURE
            Log.d(ContentValues.TAG, String.format("Command failed with state %s and rc %s.%s", session.getState(), session.getReturnCode(), session.getFailStackTrace()));

        }
    }
}